package com.sbl.bajw.pc.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.model.SignTModel;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.pc.service.SignTModelService;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/SignTModel")
public class SignTModelAction {

	@Resource
	private SignTModelService service;
	
	@Resource
	private LogTModelService logService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("SignTModel");
	}
	
	@RequestMapping(value = "/sign")
	public @ResponseBody String sign(HttpServletRequest request, HttpServletResponse response) throws MessageException{
		String userId = RequestUnit.getString("user_id");
		String Addr = RequestUnit.getString("address");
		String positionX = RequestUnit.getString("position_x");
		String positionY = RequestUnit.getString("position_y");
		String json = "";
		//插入数据
		SignTModel model = new SignTModel();
		LogTModel logT = new LogTModel();
		Date d = new Date();
		try {
			log.info("签到参数：userId="+userId+",Addr="+Addr+",positionX="+positionX+",positionY="+positionY);
			model.setAddress(Addr);
			model.setUserid(userId);
			model.setSignPositionX(positionX);
			model.setSignPositionY(positionY);
			service.insert(model);
			
			logT.setUserid(userId);
			logT.setCode("1");
			logT.setOperatingContent(sdf.format(d) + "," + userId + "在" + Addr + "签到");
			if(logService.getSignNum(model.getUserid()) < 5){
				logT.setNote("1");
			}
			logService.insert(logT);
			
			json = JsonResultUtil.Success("签到成功");
		} catch (Exception e) {
			log.error("签到失败", e);
			throw new MessageException("签到失败", "3");
		}
		return json;
	}
	
	@RequestMapping(value = "/querySignHistory")
	public @ResponseBody String querySignHistory(HttpServletRequest request, HttpServletResponse response){
		String userId = request.getParameter("user_id");
		String startDate = RequestUnit.getString("start_date");
		String stopDate = RequestUnit.getString("stop_date");
		List<HashMap<String, Object>> result = null;
		HashMap<String, Object> parm = new HashMap<String, Object>();
		if(userId != null && !"".equals(userId)){
			parm.put("UserId", userId);
			parm.put("startDate", startDate);
			parm.put("stopDate", stopDate);
			log.info("查询某月历史签到：userId="+userId+",startDate="+startDate+",stopDate="+stopDate);
			result = service.getModelList(parm);
		}
		String json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(result));
		return json;
	}
	
	@RequestMapping(value = "/querySignHistoryByOneDay")
	public @ResponseBody String querySignHistoryByOneDay(HttpServletRequest request, HttpServletResponse response){
		String date = RequestUnit.getString("date");
		String userId = request.getParameter("user_id");
		HashMap<String, Object> parm = new HashMap<String, Object>();
		List<HashMap<String, Object>> str = null;
		String useridR = request.getParameter("user_id")+"";
		if(!useridR.equals(userId)){
			log.info("error >> "+useridR+","+userId);
		}
		if(userId != null && !"".equals(userId)){
			parm.put("userId", userId);
			if(date != null && !"".equals(date)){
				parm.put("startDate", date + " 00:00:00");
				parm.put("stopDate", date + " 23:59:59");
				log.info("查询某天历史签到：date=" + date + ",userId=" + userId);
			}else{
				Date d = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				parm.put("startDate", sdf.format(d) + " 00:00:00");
				parm.put("stopDate", sdf.format(d) + " 23:59:59");
				log.info("查询当天历史签到：date=" + sdf.format(d) + ",userId=" + userId);
			}
			//历史记录查询
			str = service.getSignTModel(parm);
		}
		String json = "";
		json = JsonResultUtil.Success("", JsonUtil.ConvertArrayToJson(str));
		return json;
	}
	
	@RequestMapping(value = "/deleteSign")
	public @ResponseBody String deleteSign(HttpServletRequest request, HttpServletResponse response){
		String id = RequestUnit.getString("id");
		String userId = request.getParameter("user_id");
		//删除操作
		HashMap<String, Object> parm = new HashMap<String, Object>();
		LogTModel logT = new LogTModel();
		Date d = new Date();
		if(id != null && !"".equals(id)){
			log.info("删除当天签到记录：id="+id+",userId="+userId);
			parm.put("id", id);
			service.deleteById(parm);
			
			logT.setUserid(userId);
			logT.setCode("2");
			logT.setOperatingContent(sdf.format(d) + "," + userId + "删除签到");
			logService.insert(logT);
		}
		String json = "";
		json = JsonResultUtil.Success("删除成功");
		return json;
	}
}
